<?php
function QttUidGet(){//趣头条唯一标识openid获取
    $appid=cookie('QTTappid');
    $arrkey=QttGetKey($appid);
    $platform='qtt';
    $ticket=cookie('QTTticket');
    $time=time();
    $params=array(
        'app_id'=>$appid,
        'platform'=>$platform,
        'ticket'=>$ticket,
        'time'=>$time,
        'app_key'=>$arrkey,
    );
    unset($params['sign']);
    $params["app_key"] = $arrkey;
    ksort($params, SORT_NATURAL);
    $sign = '';
    foreach ($params as $k => $v) {
        $sign .= $k . $v;
    }
    unset($params["app_key"]);
    $sign= md5($sign);

    $url="https://newidea4-gamecenter-backend.1sapp.com/x/open/user/ticket?app_id={$appid}&platform={$platform}&ticket={$ticket}&time={$time}&sign={$sign}";

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
    $json = curl_exec($ch);

    $Arr=json_decode($json);
    cookie('QTTopenid',$Arr->data->open_id);
}
function QttGetKey($appid){//获取key
    $Arr=array(
        'a3ycij79o8k7'=>'TETYd71hNlhygFCQgGAFEYgOswElCQZgQ8uyJgh1FnggkuQCgYOJoNEQCwCxoTTE',//八字精批
        'a3zaiwu6rC6o'=>'GSOGJuIIJBmGXKyO6CXFEFXKQOGVBzHJYqQ4J8Jg43EuFOQJkCPLsQKAuuHVXuzQ',//八字合婚
        'a3zaiymvhB4o'=>'UzxMLWxLALwBpIUUhvExIFUIAMD4WZAULBbL4WdGojYUYCEZ1WTUDJTzZIMYEDCE',//八字事业
        'a3zaizLQWdzE'=>'EBEVLFLIKAdOEDRAwt3RLRAKVR71bA1A5WyFDtW1hQOBRQGMFBMRMdqLrO7R4HOG',//婚缘走势
        'a3zaiKB2Gv39'=>'QMjtYIEDEjDULYdBDb9NqCTzKVLBMu6Gc5QYBLt6EPYPLPMD9LcMkljDLPh1giLg',//八字财运
        'a3zaiNp5EuSL'=>'WaCICKJs3CHJG64C3SEaQDHCuQAaYYNYAswUHx3YXifWTQsXERCayC71QCB7CzCX',//姓名测算
        'a3zaiPNrB5Sm'=>'yI3FZCMtFNXVQWMHqxXSyXXuHZFV1L464uSJXktyMEtBDMuF1VTdXCzbamJFd3Bg',//星座测算
        'a3zaiU4ed2U5'=>'yTrCLKRiJLNKisxFskiQsEEip6QF4TgcgHgPFK1CJA1id9oPLXATDLi3Aj334MIu',//号码解析
        'a3zaiVEpsoUy'=>'gaLC4GHAgcScNVzg9jNMbLEEXccqCfbEVfzTfMNKpg8LglMfSoNc74PPIfrDaFk1',//桃花运
        'a3zaiXmSAcfq'=>'iZd2SxWaZdWWFiEeEOD2GYAYZhcz3QXCNeygNZ75w2YZ2iEdm21KDKtK2TiQYqeP',//2019流年运程
        'a3zaoWjrLjpj'=>'TPFTMwuXhfhjEnnSuPSJ8u9AFCSnFFNQCwr7COhJABcwBVvOSn1N4tONuzFqfN1C',//每月运势
        'a3ASoDxYKvBE'=>'fzAJGL7oUa3RpPBEsakysXtGAKXW5HUnnidzLdcAkcfknC971z9kpGeR9OBG7cEG',//宝宝起名
        'a3ASoMSHmEbU'=>'ZsCbEYhHpOI5fH2nWakEZhEGoCUfdEeHoEhhtPChxdNFHn2EbZFACEAzqyoQfORN',//紫微斗数
        'a3ASoTmo4gEQ'=>'a1iygeehVzso9xHsEJRKVgTsGF7VVewfoces9GO2EPKD4AGpbns5bAkOKKn42ATs',//麻将每日牌运
        'a3zaiuGX16bq'=>'DOsOJSFmSAiH9HScMOUEAPd8GPdid7SHdIBMdKiiZEirgEoHOEAH7sMNyPAyFIdw',//多余的八字精批
    );
    return $Arr[$appid];
}

function toutiaoJXuser(){//头条精选   用户信息
    $userId=cookie('uid');
    $gameId=3273;
    $merchantId=3273;
    $timestamp=time();
    $key='620D4A925E2E5B34';
    $strData="gameId={$gameId}@merchantId={$merchantId}@timestamp={$timestamp}@userId={$userId}@key={$key}";
    $sign=strtoupper(md5($strData));
    $strData=array(
        'gameId'=>$gameId,
        'merchantId'=>$merchantId,
        'timestamp'=>$timestamp,
        'userId'=>$userId,
        'sign'=>$sign
    );
    $url="https://game.deshangkeji.com/Api/Novel/Info";
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
    curl_setopt($ch,CURLOPT_POST,true); //post请求
    curl_setopt($ch,CURLOPT_POSTFIELDS,$strData);// post请求的数据
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
    $json = curl_exec($ch);
    $json=json_decode($json,true);
}

function jumpZhifu(){//138结果跳支付功能
    $birthday='0-'.cookie('zymd').'-'.cookie('zhour');
    $username=cookie('znickname');
    $gender=cookie('zsex');
    $uid=cookie('uid');
    $channel=cookie('channel');
    $arrData=array(
        1=>array(
            'typeName'=>'性格分析',
            'text'=>'你的性格决定命运，对平时工作、人际和生活等多方面非常深的影响。通过分析性格优劣势找出你目前存在的不足，扬长避短才能避免失败。因此你千万不能错过！',
            'img'=>'https://hy.yixueqm.com/zhiming/Public/images/bzjp_img/v3/xingge@2x.png',
            'url'=>"https://hy.yixueqm.com/zhiming/index.php/home-zwds-zwds_zhifu?birthday={$birthday}&username={$username}&gender={$gender}",
        ),
        2=>array(
            'typeName'=>'财运分析',
            'text'=>'财富非定数，发达有玄机，通过你的八字命盘了解你的财富格局，你在2019年有哪些发财的机会？如何才能在财运好的年份抓住财源，你一定要看！',
            'img'=>'https://hy.yixueqm.com/zhiming/Public/images/bzjp_img/v3/caiyun@2x.png',
            'url'=>"https://hy.yixueqm.com/zhiming/index.php/home-bzcy-bzcy_zhifu?birthday={$birthday}&username={$username}&gender={$gender}",
        ),
        3=>array(
            'typeName'=>'爱情恋爱建议',
            'text'=>'你是否还在为爱情困惑？烂桃花多？正缘难遇？通过你的八字命盘深度鉴定你的恋爱桃花运，预知你的爱情走向，从而把握命中正缘的最佳时机。',
            'img'=>"https://hy.yixueqm.com/zhiming/Public/images/bzjp_img/v3/ganqing@2x.png",
            'url'=>"https://hy.yixueqm.com/zhiming/index.php/home-lath-lath_zhifu?birthday={$birthday}&username={$username}&gender={$gender}",
        ),
        4=>array(
            'typeName'=>'健康建议',
            'text'=>'通过八字命盘分析你的身体健康信息，身体内是否存在潜在的病症信息，及时发现潜在疾病，真正做到防患于未然！',
            'img'=>'https://hy.yixueqm.com/zhiming/Public/images/bzjp_img/v3/jiankang@2x.png',
            'url'=>"https://hy.yixueqm.com/quce/index.php/home-paycs-paypage?subject=QWCS22F&id=130&uid={$uid}&channel={$channel}&birthday={$birthday}&username={$username}&gender={$gender}&jumpType=1",
        ),
        5=>array(
            'typeName'=>'事业成就',
            'text'=>'通过八字排盘全面剖析你的个人事业运势，今年是否有升迁变动？是否会有小人阻碍？什么时候有贵人相助？根据以上因素，为您的事业提供实际指引和建议。你一定要看！',
            'img'=>'https://hy.yixueqm.com/zhiming/Public/images/bzjp_img/v3/shiye@2x.png',
            'url'=>"https://hy.yixueqm.com/zhiming/index.php/home-bzsy-bzsy_zhifu?birthday={$birthday}&username={$username}&gender={$gender}",
        ),
        6=>array(
            'typeName'=>'紫微斗数',
            'text'=>'以上是您的紫微命盘，其中蕴藏着您的财运、婚姻情感、事业发展、人际关系等很多秘密。大师通过解读你的紫薇命盘，分析你的流年运势，你千万不能错过。',
            'img'=>'https://hy.yixueqm.com/zhiming/Public/images/bzjp_img/v3/yunshi@2x.png',
            'url'=>"https://hy.yixueqm.com/zhiming/index.php/home-zwds-zwds_zhifu?birthday={$birthday}&username={$username}&gender={$gender}",
        ),
    );
    return $arrData;
}

function channelxinbang($channel){//合作新榜渠道判断
    $xbArr=S('xb_channel_uuid');
    if(empty($xbArr)){
        $xbArr=M()->query("select channel from xb_channel_uuid where id>=1");
        S('xb_channel_uuid',$xbArr,10);
        xinbangUpdateOrder();
    }
    foreach($xbArr as $key=>$value){
        if($value['channel']==$channel){
            return 1;
            break;
        }
    }
}
function xinbangUpdateOrder(){//新榜7天更新订单
    if(S('xinbangUpdateOrder')!=1){
        $oldDayTime=strtotime('-7 day');
        $oldDay=date('Y-m-d H:i:s',$oldDayTime);
        M()->query("update xb_order set indent_status=4 where distribution_time<='{$oldDay}' and indent_status=1");
        S('xinbangUpdateOrder',1,1800);
    }
}
function wanyuProductId(){//万娱 商品id获取
    $channel=cookie('channel');
    if($channel=='YSWNL_iOS'){
        $orderidnum=cookie('orderidnum');
        $Arr=array(
            'Bzjp'=>array('name'=>'八字精批','productId'=>'com.yswnl.bzjp'),
            'Bzhh'=>array('name'=>'八字合婚','productId'=>'com.yswnl.bzhh'),
            'Bzsy'=>array('name'=>'八字事业运','productId'=>'com.yswnl.bzsy'),
            'Hyzs'=>array('name'=>'婚缘走势','productId'=>'com.yswnl.hyzs'),
            'Bzcy'=>array('name'=>'八字财运','productId'=>'com.yswnl.bzcy'),
            'Xmcs'=>array('name'=>'姓名测算','productId'=>'com.yswnl.xmcs'),
            'Lnyc2020'=>array('name'=>'流年运程','productId'=>'com.yswnl.lnyc'),
            'Zwds'=>array('name'=>'一生运势','productId'=>'com.yswnl.ywds'),
        );
        $productId=$Arr[$orderidnum]['productId'];
        if(empty($productId)){$productId=$Arr['Lnyc2020']['productId'];}
        cookie('WYproductId',$productId);
        //dump(cookie('WYproductId'));
    }else{

    }
}

function H5WeixinPay($orderid,$price,$name){//合作H5支付接口
    $mweb_url=S($orderid.'mweb_url');
    if($mweb_url){
        return $mweb_url;
    }

    $price*=100;
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {$ip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {$ip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {$ip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {$ip = $_SERVER['REMOTE_ADDR'];
    }
    $ip=preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';

    $scene_info="{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"https://hy.yixueqm.com\",\"wap_name\": \"知命支付\"}}";

    $channel=cookie('channel');
    $noncestr=noncestr(15);
    $KEY=C('KEY');

    $APPID=C('APPID');
    $MCHID=C('GMCHID');

    $body=$name;
    $type='MWEB';
    $notifyUrl="http://{$_SERVER['HTTP_HOST']}/zhiming/index.php/Home-Index-notify_urlwx";
    $stringA="appid=".$APPID."&body={$body}&mch_id={$MCHID}&nonce_str={$noncestr}&notify_url={$notifyUrl}&out_trade_no={$orderid}&scene_info={$scene_info}&spbill_create_ip={$ip}&total_fee={$price}&trade_type={$type}";
    $stringSignTemp=$stringA."&key=".$KEY; //注：key为商户平台设置的密钥key
    $sign=strtoupper(md5($stringSignTemp));

    $strData=array(
        'appid'=>$APPID,
        'mch_id'=>$MCHID,
        'nonce_str'=>$noncestr,
        'sign'=>$sign,
        'body'=>$body,
        'out_trade_no'=>$orderid,
        'total_fee'=>$price,
        'spbill_create_ip'=>$ip,
        'notify_url'=>$notifyUrl,
        'trade_type'=>$type,
        'scene_info'=>$scene_info,
    );

    $xml = "<xml>";
    foreach ($strData as $key=>$val)//数组转xml
    {
        if (is_numeric($val)){
            $xml.="<".$key.">".$val."</".$key.">";
        }else{
            $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
        }
    }
    $xml.="</xml>";
    $strData=$xml;
    //$json=json_encode($data,JSON_UNESCAPED_UNICODE);
    $headers=array(
        'Content-Type:text/xml;charset=utf-8',
    );
    $url="https://api.mch.weixin.qq.com/pay/unifiedorder";

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch,CURLOPT_URL,$url); //设置请求地址
    curl_setopt($ch,CURLOPT_POST,true); //post请求
    curl_setopt($ch,CURLOPT_POSTFIELDS,$strData);// post请求的数据
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//不需要证书验证
    curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);//不直接输出到页面
    $json = curl_exec($ch);
    $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

    $xmlstring = simplexml_load_string($json, 'SimpleXMLElement', LIBXML_NOCDATA);
    $val = json_decode(json_encode($xmlstring),true);
    curl_close($ch);

    if(empty($val['mweb_url'])){
        if(mb_substr($val['err_code_des'],0,3)==201){
            $wzurl=wxJsdkData();
            echo $val['err_code_des'];
            redirect($wzurl);
        }
        dump($val);echo 'H5ZMwxzhifu';exit;
    }

    $mweb_url=$val['mweb_url'];
    if($val['mweb_url']){
        S($orderid.'mweb_url',$mweb_url,290);
        return $mweb_url;
    }
}


